<?php
namespace app\api\controller;
use app\service\apiService;
use app\service\orderService;
use think\Db;
class Upload{
	//车辆入场
	public function carInData(){
		$postdata=request()->post();
		//校验是否缺少参数
		
		$parkinglotinfo=Db::table("parkinglotinfo")->where("parking_id",$postdata["parkingCode"])->find();
		$systeminfo=Db::table("system")->where("system",$postdata["serialno"])->find();
		if(empty($parkinglotinfo)){
			return '{"resultCode": "400","errmsg": "停车场信息不存在"}';
			return "停车场信息不存在";
		}
		if(empty($parkinglotinfo)){
			return '{"resultCode": "400","errmsg": "相机信息不存在"}';
			return "相机信息不存在";
		}
		$data["car_number"]=$postdata["car_number"];
		$data["uid"]=$postdata["uid"];
		$data["parking_name"]=$parkinglotinfo["parking_name"];
		$data["parking_id"]=$parkinglotinfo["parking_id"];
		$data["out_parking_id"]=$parkinglotinfo["out_parking_id"];
		$res=Db::table("car_order")->where("parking_id",$data["parking_id"])->where("uid",$data["uid"])->find();
		if($res){
			(new orderService())->delOrder($res["id"]);
		}
		$api = new apiService();
		$result = $api->enterinfo(json_encode(['parking_id' => $data['parking_id'], 'car_number' =>  $data['car_number'], 'in_time' => $postdata["in_time"]]));
        //判断是否上传成功
		if(property_exists($result,'sub_msg')){
			return '{"resultCode": "500","msg": '.$result->sub_msg.'}';
		}else{
			
			$data["in_time"]=strtotime($postdata["in_time"]);
			$data['in_system']=$systeminfo["system"] ;
			$data["in_system_name"]=$systeminfo["system_name"];
            $orderinadd = (new orderService())->orderInAdd($data);
			if($orderinadd){
				$car_record_entry["out_parking_id"]=$parkinglotinfo["out_parking_id"];
				$car_record_entry["parking_name"]=$parkinglotinfo["parking_name"];
				$car_record_entry["car_number"]=$data["car_number"];
				$car_record_entry["system"]=$systeminfo["system"];
				$car_record_entry["system_name"]=$systeminfo["system_name"];
				$carEntryRecordAdd=Db::table("car_record_entry")->insert($car_record_entry);
				return '{"resultCode": "200","msg": "操作成功"}';
			}else{
				return '{"resultCode": "400","errmsg": "操作失败"}';
			}
		}
		return json_encode($data);
	}

	//车辆出场
	public function carOutData(){
		$postdata=request()->post();
		
		//校验是否缺少参数
		
		$parkinglotinfo=Db::table("parkinglotinfo")->where("parking_id",$postdata["parkingCode"])->find();
		$systeminfo=Db::table("system")->where("system",$postdata["serialno"])->find();
		if(empty($parkinglotinfo)){
			return '{"resultCode": "400","errmsg": "停车场信息不存在"}';
			return "停车场信息不存在";
		}
		if(empty($parkinglotinfo)){
			return '{"resultCode": "400","errmsg": "相机信息不存在"}';
			return "相机信息不存在";
		}
		
		//记录出场数据到comeout
		$comeout['parking_id'] = $parkinglotinfo['out_parking_id'];//停车场ID
		$comeout['system'] = $systeminfo["system"];//设备号
		$comeout['car_number'] = $postdata['car_number'];//车牌号码
		$out_time=time();
		$comeout['out_time'] = $postdata["out_time"];//出场时间
		//根据停车场ID和设备号查询是否有出场
		$comeoutinfo = (new orderService())->comeoutInfo($comeout['system']);
		if(empty($comeoutinfo)){
			$comeoutadd = (new orderService())->comeoutAdd($comeout);//添加
		}else{
			$comeout['id'] = $comeoutinfo['id'];//id
			$comeoutedit = (new orderService())->comeoutEdit($comeout);//添加
		}
		
		$orderinfo=Db::table("car_order")->where("parking_id",$parkinglotinfo["parking_id"])->where("uid",$postdata["uid"])->find();
		if(empty($orderinfo)){
			return '{"resultCode": "404","errmsg": "订单信息不存在"}';
		}
		//更新订单信息
		$out['id'] = $orderinfo['id'];
		$out['order_time'] = date('Y-m-d H:i:s',time());
		$out["pay_money"]=$postdata["ordermoney"];
		$out['out_order_no']=date('YmdHis').rand(10000,99999);
		$out['out_time']=$postdata['out_time'];
		$out['in_duration']="";
		$out["roadgate"]=$systeminfo["roadgate"];
		$out['out_system']=$systeminfo["system"] ;
		$out["out_system_name"]=$systeminfo["system_name"];
		$orderedit = (new orderService())->orderEdit($out);
		if($orderedit){
			return '{"resultCode": "200","errmsg": "操作成功"}';
		}
	}
	
	//申请扣款
	public function applypay(){
		
	}


}